package com.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.SessionData;
import com.server.service.IMngLoginService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

/**
 * 后台登陆控制器
 * 
 * @author xiaoxiaoping
 * @date 2016年7月19日
 */
@Controller("MngLoginController")
@RequestMapping("admin")
public class MngLoginController {
	final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private IMngLoginService mngLoginService;
	
	@RequestMapping("")
	public ModelAndView loginPage(){
		ModelAndView mv = new ModelAndView("mngLogin/login");
		return mv;
	}
	@RequestMapping("login")
	public  void login(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		Map<String, Object> loginMap = new HashMap<String, Object>();
		Boolean isSuccess = false;
		String info = null;
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		String verifyCode = session.getAttribute("code").toString();
		if(!code.equalsIgnoreCase(verifyCode)){
			isSuccess = false;
			info = "验证码错误!";
		}else{
			Map<String, Object> returnMap = mngLoginService.login(username, password);
			if((boolean) returnMap.get("isSuccess") && (boolean)returnMap.get("valid")){
				SessionUtil.setSession(request, SessionData.nameKey_mng, returnMap.get("nickname"));
				SessionUtil.setSession(request, SessionData.uuidKey_mng, returnMap.get("uuid"));
				if(returnMap.get("authority").equals("all")){
					SessionUtil.setSession(request, SessionData.levelKey_mng, "1");
				}else{
					SessionUtil.setSession(request, SessionData.levelKey_mng, "2");
				}
				isSuccess = true;
				info="登陆成功！";
				logger.info("登录成功!");
			}
			if((boolean) returnMap.get("isSuccess") && !(boolean)returnMap.get("valid")) {
				isSuccess = false;
				info="你的账号已禁用请联系管理员！";
			}
			if(!(boolean) returnMap.get("isSuccess")){
				isSuccess = false;
				info = "用户名或密码错误！";
			}
		}
		loginMap.put("isSuccess", isSuccess);
		loginMap.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(loginMap, null, null));
	}
	@RequestMapping("logout")
	public  ModelAndView logout(HttpServletRequest request){
		 SessionUtil.clear(request);
		 return new ModelAndView("mngLogin/login");
	}
	@RequestMapping("update")
	public  ModelAndView update(HttpServletRequest request){
		 return new ModelAndView("mngLogin/update");
	}
	@RequestMapping("save")
	public  void save(HttpServletRequest request,
			HttpServletResponse response){
			boolean isSuccess = false;
			Map<String, Object> returnMap = new HashMap<String, Object>();
			String password = request.getParameter("password");
			String uuid = SessionUtil.getStringAttri(request, SessionData.uuidKey_mng);
			isSuccess = mngLoginService.updatePassword(password, uuid);
			if(isSuccess){
				 SessionUtil.clear(request);
			}
			returnMap.put("isSuccess", isSuccess);
			OutputUtil.returnString(response, JSONSerielizer.toJson(returnMap, null, null));
	}
}
